/*******************************************************************************

                  LVGL 输入法公共部分实现

*******************************************************************************/
#include "lvIME_Base.h"

//存储键显示字符的字符表，不在符号输入法时可作它用
char lvIME_SignBuf[25][LV_IME_SIGN_BYTE];  //25为符号输入法容量

/*******************************************************************************
                       符号表公共部分
*******************************************************************************/
//-----------------------------得到符号表总数----------------------------
unsigned short lvIME_GetSignTblCount(const char *pSignTbl)
{
  unsigned short Count = 0;
  do{
    while(*pSignTbl == ' ') pSignTbl++; //去除多个空格
    char Sign = *pSignTbl++;
    if(Sign == '\0') return Count; //列表最后跟空格结束
    Count++;
    while(Sign != ' '){//多字节字符
      if(Sign == '\0') return Count; //列表最后跟内容结束
      Sign = *pSignTbl++;
    }
  }while(1);
  return Count;
}

//------------------------------符号缓冲区清除---------------------------------
//清为一个空格
void lvIME_ClrSignBuf(char *pSignBuf,      //符号缓冲区起始
                      unsigned char Start, //起始位(含)
                      unsigned char End)   //结束位(不含)
{
  char *pEndSignBuf = pSignBuf + End * LV_IME_SIGN_BYTE;
  pSignBuf = pSignBuf + Start * LV_IME_SIGN_BYTE;
  for(; pSignBuf < pEndSignBuf; pSignBuf += LV_IME_SIGN_BYTE){
    pSignBuf[0] = ' ';  //填充空格
    pSignBuf[1]= '\0'; //结束符
  }
}

//----------------------将符号表填充入符号缓冲区----------------------------
//返回页符号表结束位置
const char *lvIME_SignTblToBuf(char *pSignBuf,          //符号缓冲区起始
                               unsigned char SignCount, //需填充缓冲区总数
                               const char *pSignTblBase)//对应页符号表起始
{
  char *pEndSignBuf = pSignBuf + SignCount * LV_IME_SIGN_BYTE;
  for(; pSignBuf  < pEndSignBuf; pSignBuf += LV_IME_SIGN_BYTE){
    while(*pSignTblBase == ' ') pSignTblBase++; //去除多个空格间隔
    char Sign = *pSignTblBase++;
    unsigned char Pos = 0;
    while(Sign != ' '){//多字节字符
      if(Sign == '\0') break; //结束了
      if(Sign == '\n') Sign = ' ';//转义为空格
      pSignBuf[Pos++] = Sign;
      if(Pos >= (LV_IME_SIGN_BYTE - 1)) break;//异常
      Sign = *pSignTblBase++;
    }
    pSignBuf[Pos] = '\0';
    if(Sign == '\0') break; //结束了
  }
  return pSignTblBase;
}

/*******************************************************************************
                            软键盘公共部分
*******************************************************************************/
//-------------------------开始创建输入法键盘---------------------------------
//创建键盘前调用，返回创建的对像后，对像需重置大小与map等
lv_obj_t *lvIME_CreateKeyStart(lv_event_cb_t _EventHandler, //回调函数
                               void *pv)//回调函数带的参数
{
  lv_obj_t *pParent = LV_IME_KB_LAYER; //此即为顶层
  lv_obj_t * pBtnM = lv_buttonmatrix_create(pParent);
  lv_obj_add_style(pBtnM, &TMStyle_NullBG, 0); //无边界
  lv_obj_add_style(pBtnM, &TMStyle_BtnFull, LV_PART_ITEMS);//按钮铺满
  lv_obj_align(pBtnM, LV_ALIGN_BOTTOM_MID, 0, 0);
  lv_obj_add_event_cb(pBtnM, _EventHandler, LV_EVENT_VALUE_CHANGED, pv);
  lv_obj_remove_flag(pBtnM, LV_OBJ_FLAG_CLICK_FOCUSABLE);//单选功能
  return pBtnM;
}
